/* 
 * File:   TileProperties.cpp
 * Author: RedEyedKiller
 * 
 * Created on 26 Οκτώβριος 2010, 10:39 μμ
 */

#include "TileProperties.h"
#include "parsers/tinyxml.h"
#include "Globals.h"
#include "Logger.h"

TileProperties::TileProperties()
{
}

TileProperties::~TileProperties()
{
}

bool TileProperties::ParsePoperties(const char* filename)
{
    TiXML::TiXmlDocument xFile((Globals::GetInstance()->GetPath("TileProperties") + filename).c_str());
    if (!xFile.LoadFile()) return false;

    //get the root of the file : map
    TiXML::TiXmlElement* xProps = xFile.FirstChildElement("TileProperties");
    if (!xProps) return false;

    TiXML::TiXmlElement* xItem = xProps->FirstChildElement();
    unsigned long key = 0;
    while (xItem)
    {
        std::string value(xItem->Attribute("value"));
        //with each iteration we get the next binary number power of 2

        tileProperties[(1 << key)] = value;
        key++;
        {
            //free this item and fetch the next one
            TiXML::TiXmlElement* tmp = xItem->NextSiblingElement();
            xItem->Clear();
            xItem = tmp;
        }
    }
    return true;
}

unsigned long TileProperties::ValueOf(const std::string& name) const
{
    propertiesMap::const_iterator it = tileProperties.begin();
    propertiesMap::const_iterator end = tileProperties.end();
    for(;it != end;++it)
    {
        if(it->second == name)
            return it->first;
    }
    LOGERROR << "Name : " << name <<" not found in tile properties";
    return 0;
}

std::string TileProperties::NameOf(unsigned long flag)
{
    return tileProperties[flag];
}
